//
// Created by abstergo on 24-11-5.
//

#ifndef TBS_CPP_INCLUDE_BASE_THREADPOOL_H
#define TBS_CPP_INCLUDE_BASE_THREADPOOL_H
#include <base/base.hpp>
#include <functional>

using task_function = std::function<void(void *data, unsigned long size,const char* threadName,unsigned long long taskId)>;

CONST_EXP int TaskStatus_FINISH = 1, TaskStatus_RUNNING = 2, TaskStatus_Error = 0, TaskStatus_Cancel = 3,
	TaskStatus_Wait = 4;
struct Task {
  task_function task;
  void *data;
  unsigned long dataSize = 0;
  unsigned long long taskId;
  int status = TaskStatus_Wait;
  Task(const task_function &Task, void *Data = nullptr, unsigned long DataSize = 0, unsigned long long int TaskId = 0);
};

using reject_function = std::function<void(CONST char *threadName,
										   CONST Task *task,
										   const char *reason)>;

void logReject(CONST char *threadName,
			   CONST Task *task,
			   const char *reason);

struct ThreadPoolConfig {
  tbs::str_type threadName = "default";
  int maxTaskCount = -1;
};

Task createTask(task_function task, unsigned long long taskId = 0, void *data = nullptr, unsigned long dataSize = 0);

void useThreadPool(std::initializer_list<ThreadPoolConfig> configs, reject_function r = logReject);

Task *submitTask(CONST Task &t);

#endif //TBS_CPP_INCLUDE_BASE_THREADPOOL_H
